package apka.SMSBillet.Activities;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import apka.SMSBillet.R;
import apka.SMSBillet.SMSManager;
import apka.SMSBillet.CTR.CTRcity;
import apka.SMSBillet.CTR.CTRdatabase;
import apka.SMSBillet.R.array;
import apka.SMSBillet.R.id;
import apka.SMSBillet.R.layout;

public class BuyTicket extends Activity{
	Button bFindTravel;
	Spinner spFrom = null;
	Spinner spTo = null;
	Spinner spType = null;
	TextView tVZones = null;
	TextView tVPrice = null;
	TextView tVWaitingForSMS = null;
	ProgressBar pbWaitingForSMS = null;
	Button btnBuyBack = null;
	Button btnBuyTicket = null;
	String sFrom = null;
	String sTo = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.buy_ticket);
	    initiateViews();
	    populateSpinners();
	    setListeners();
	    loadLastActivity();
	}
	

	private void setListeners() {		
		spType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
//				calcPrice(getZonesTo());
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnBuyTicket.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sendSMS("60502442", composeSms(getCurrentZone(),getZonesTo()));
				setProgressBar();
			}
		});

	}
	

	private int getCurrentZone(){
		CTRdatabase ctrDb = new CTRdatabase(this);
		String hej = (String) spFrom.getSelectedItem();
		return ctrDb.getCityId(hej);
	}
	
	private int getZonesTo(){
		CTRdatabase ctrDb = new CTRdatabase(this);
		String testFrom = (String) spFrom.getSelectedItem();
		String testTo = (String) spTo.getSelectedItem();
		
		return ctrDb.getZonesBetween(testFrom, testTo);
	}
	
	
	private String composeSms(int currentZone, int amountOfZones){
		int type = spType.getSelectedItemPosition();
		String composedSMS = null;
		switch(type){
			case 0: composedSMS = currentZone +" "+ amountOfZones;
				break;
			case 1: composedSMS = currentZone +" "+ amountOfZones+" "+"N";
				break;
			case 2: composedSMS = currentZone +" "+ amountOfZones+" "+"B";
				break;
			case 3: composedSMS = currentZone +" "+ amountOfZones+" "+"P";
				break;
			case 4: composedSMS = currentZone +" "+ amountOfZones+" "+"PN";
		}
		return composedSMS;
	}
	
	// Composes SMS to NT
	private void sendSMS(String phoneNumber, String message) {
		PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
				ConfirmTicket.class), 0);
//		SmsManager sms = SmsManager.getDefault();
//		sms.sendTextMessage(phoneNumber, null, message, pi, null);
		
		SMSManager.getInstance();
		SMSManager.sendSMS(phoneNumber, message, null);
	}

	public void loadLastActivity(){
		Intent callingIntent = getIntent();
		sFrom = callingIntent.getStringExtra("FROM");
		sTo = callingIntent.getStringExtra("TO");
		
		spType.setSelection(callingIntent.getIntExtra("TYPE", 0));
		
		CTRcity ctrCity = new CTRcity(this);
		String[] cities = ctrCity.getCities();
		spFrom.setSelection(getIdFromCity(sFrom, cities));
		spTo.setSelection(getIdFromCity(sTo, cities));
	}
	
	public int getIdFromCity(String city, String[] stringArray){
		CTRcity ctrCity = new CTRcity(this);
		int id = ctrCity.getIdForCity(city, stringArray);
		return id;
	}
	
	
    public void initiateViews(){
    	btnBuyTicket = (Button) findViewById(R.id.btn_buy_travel);
    	tVPrice = (TextView) findViewById(R.id.tV_price);
    	tVZones = (TextView) findViewById(R.id.tV_zones);
    	spFrom = (Spinner) findViewById(R.id.spinner_from);
    	spTo = (Spinner) findViewById(R.id.spinner_to);
    	spType = (Spinner) findViewById(R.id.spinnerTypeBuy);
    	tVWaitingForSMS = (TextView) findViewById(R.id.tV_Confirm_Waiting_for_sms);
    	pbWaitingForSMS = (ProgressBar) findViewById(R.id.progress_confirm_waiting_for_sms);
    }
    
    private void setProgressBar(){
    	pbWaitingForSMS.setVisibility(0);
    	pbWaitingForSMS.bringToFront();
    	pbWaitingForSMS.isIndeterminate();
    	tVWaitingForSMS.setText("Afventer svar fra NT");
    }
    
    public void populateSpinners(){
    	ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.types, android.R.layout.simple_spinner_item);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spType.setAdapter(adapter);

    	//TODO: Make call to CTRCity to obtain sorted array
    	/*	
    	 *     CTRdatabase ctrDb= new CTRdatabase(this);
    	 *     String[] stringArray = ctrDb.sortArray(ctrDb.fillCityArray());
    	 * 
    	 */
    	CTRcity ctrCity = new CTRcity(this);
   	
    	ArrayAdapter<String> adapterFrom = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ctrCity.getCities());
    	adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spFrom.setAdapter(adapterFrom);
    	
    	ArrayAdapter<String> adapterTo = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ctrCity.getCities());
    	adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spTo.setAdapter(adapterTo);
    }


    
    
}
